package com.prs.crm.dao.cdxh.impl;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.prs.crm.dao.cdxh.DistributionOrderDetailDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.service.cdxh.cd_ProductService;


public class DistributionOrderDetailDaoImpl extends BaseDaoImpl<DistributionOrderDetail> implements DistributionOrderDetailDao {

	private cd_ProductService cd_ProductService;
	@Override
	public DistributionOrderDetail getDistributionOrderDetail(String productCode) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> findProductCode() {
		// TODO Auto-generated method stub
		 return (List<String>)this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrderDetail.class);
				           //投影
				        	criteria.setProjection(Projections.distinct(Projections.property("productCode")));
				        	
				        	return criteria.list();
				        	
				        }
					}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DistributionOrderDetail> findproToNums(final String codes) {
		// TODO Auto-generated method stub
		 return (List<DistributionOrderDetail>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrderDetail.class);
				        	criteria.add(Restrictions.eq("productCode", codes));
				     
				        	
							return criteria.list();
				        	
				        	
				        }
					}
			);
	}


	@Override
	public void saveStockNums(cd_Product cd_product) {
		// TODO Auto-generated method stub

		Integer TolNums;
        List<String> codes= this.findProductCode();
      
		if (codes.size()!=0){
			for(int i=0;i<codes.size();i++){
				int num=0;			
			
				List<DistributionOrderDetail>	details=findproToNums(((List<String>) codes).get(i));
			
				for(int j=0;j<details.size();j++){					
					 num+=details.get(j).getProToNums();					
				  }
				TolNums=num;
			
				cd_Product ds=this.getCd_ProductService().getProductObject(codes.get(i));			
				
				ds.setStockNums(TolNums);
				
			
				this.getCd_ProductService().saveOrProduct(ds);
				
			}
		}
		
		
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}


	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}


	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DistributionOrderDetail setDistributionOrderDetail(
			final String saleOrderPlan,final String productCode) {
		return (DistributionOrderDetail)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrderDetail.class);
				        	criteria.add(Restrictions.eq("saleOrderPlan", saleOrderPlan));
				        	criteria.add(Restrictions.eq("productCode", productCode));				        	
				        	List<DistributionOrderDetail> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<DistributionOrderDetail> getDistributionOrderDetails(
			final DistributionOrder orderCodePlan) {
		return (List<DistributionOrderDetail>)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrderDetail.class);
				        	criteria.add(Restrictions.eq("distributionOrder", orderCodePlan));			        	
				        	List<DistributionOrderDetail> result = criteria.list();
				        	
				        		return result;
				        	
				        }   
				 } );
	}

}